<?php


namespace app\admin\controller;


use app\common\lib\Auth;
use think\Db;
use think\Request;
use app\common\lib;

class Car extends Base
{
    public function car_list()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['username'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($type_name) && $type_name!="")
            {
                $map['type_name'] = $type_name;
            }
            if(isset($home_name) && $home_name!="")
            {
                $map['home_name'] = $home_name;
            }
            if(isset($index_name) && $index_name!="")
            {
                $map['index_name'] = $index_name;
            }
            if(isset($status) && $status!="")
            {
                $map['status'] = $status;
            }
            if(isset($keyword) || isset($type_name) || isset($home_name) || isset($index_name) || isset($status)) {
                try {
                    $count = Model('Car')
                        ->join('bd_home ', ' bd_car.user_id = bd_home.id')
                        ->field('bd_car.id,bd_home.username,bd_home.type_name,bd_home.index_name,bd_home.home_name,bd_car.status,bd_home.car_number')
                        ->page($NowPage, $limits)
                        ->where($map)
                        ->count();  //总条目数据
                    $lists = Model('Car')
                        ->join('bd_home ', ' bd_car.user_id = bd_home.id')
                        ->field('bd_car.id,bd_home.username,bd_home.type_name,bd_home.index_name,bd_home.home_name,bd_car.status,bd_car.car_number')
                        ->page($NowPage, $limits)
                        ->where($map)
                        ->select();  //当前页
                } catch (\Exception $e) {
                    return json(['code' => 500, 'msg' => $e->getMessage()]);
                }
                return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => '0', 'data' => []]);
        }
        $_type  = Model('MemberType')->get_status_all_msg('1');   //楼栋
        $_index = Model('MemberIndex')->get_status_all_msg('1');   //单元门
        $_home  = Model('MemberHome')->get_status_all_msg('1');   //房间号
        $this->assign([
            '_type' => $_type,
            '_index' => $_index,
            '_home' => $_home,
        ]);
        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }


    public function car_list_edit()
    {
        if(\request()->isAjax()){
            $data = input('post.');
            //更新数据
            $_home_msg = Model('Car')->save(['status'=>$data['status']],['id'=>$data['id']]);
            if(!$_home_msg) {
                return show('200','数据库错误!','0');
            }
            return show('200','更新成功！','1');
        }else {
            $id = Request::instance()->param('id');
            $this_data = Model('Car')->get(['id'=>$id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            return $this->fetch();
        }
    }

    public function car_list_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('Car')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }
}